%!TEX program = xelatex
%!TEX encoding = UTF-8 Unicode

\documentclass[10pt,a4paper]{article} 

\usepackage{ctex} % 中文支持
\usepackage[top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry} % 页边距
\usepackage{amsmath, amssymb} % 数学公式与符号
\usepackage{graphicx}
\usepackage{pythonhighlight}
\usepackage{url} 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{titling}
\setlength{\droptitle}{-2cm} % 标题上移
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\author{SSK ET AL }
\title{Python使用入门习题 - 我的解答 }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\maketitle

%\begin{abstract}
%Python编程练习
%\end{abstract}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\setcounter{tocdepth}{2}
%\renewcommand\contentsname{目录}
%
%\renewcommand {\baselinestretch} {1.3}\normalsize 
%\tableofcontents 
%\renewcommand {\baselinestretch} {1.0}\normalsize


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\section{}
\begin{enumerate}
\item  
在同一个图形界面，画出如下三个函数的图形，并进行标注：
$$y=\text{ch} x, y= \text{sh} x, y=e^x/2. $$

解答：
我们使用 python 的 matplotlib 库的 plot 函数画这些函数的图形。
首先定义自变量 $x$ 的取值范围 $[-3,3]$, 分成49等分。
然后计算三个函数的函数值。
然后调用函数画出图形。

\begin{python}
import pylab as plt
import numpy as np

plt.rc('font',family='SimHei')  #用来正常显示中文标签
plt.rc('axes',unicode_minus=False)  #用来正常显示负号
x=np.linspace(-3,3,50)
y1=np.cosh(x)
y2=np.sinh(x)
y3=np.exp(x)/2
plt.plot(x,y1,'r-*',label="双曲余弦函数")
plt.plot(x,y2,'--.b',label="双曲正弦函数")
plt.plot(x,y3,'-.dk',label="指数函数")
plt.legend()
\end{python}

\begin{figure}[ht!]
\centering
\includegraphics[height=5cm, width=8cm]{ti-2-1.png}
\caption{第1题的三个函数的图形}
\end{figure}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\item  
画出 $\Gamma$ 函数 $\Gamma(x)=\int_0^\infty e^{-t}t^{x-1}dt$ 的图形。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\item  
在同一个图形界面中，分别画出6条曲线：
$$y=kx^2+2k, k=1,2,\cdots,6. $$

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\item  
把屏幕开成2行3列6个子窗口，每个子窗口画一条曲线，画出曲线
$$y=kx^2+2k, k=1,2,\cdots,6. $$

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\item  
分别画出下列二次曲面：
\begin{enumerate}
\item 单叶双曲面 $\frac{x^2}{4}+\frac{y^2}{10}-\frac{z^2}{8}=1$;
\item 椭圆抛物面 $\frac{x^2}{4}+\frac{y^2}{6}=z$.
\end{enumerate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\item  
附件1：区域高程数据.xlsx给出了某区域的高程数据，画出该区域的三维表面图和等高线图，
在$A(30,0)$和$B(43,30)$（单位：公里）点处建立了两个基地，在等高线图上标注出这两个点。并求该区域地表面积的近似值。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\item  
先判断下列线性方程组的解的情况，然后求对应的唯一解、最小二乘解、或最小范数解。
\begin{eqnarray*}
(1)\left\{\begin{array}{rcl}
4x_1+2x_2-x_3 &=& 2, \\
3x_1-x_2+2x_3 &=& 10, \\
11x_1+3x_2 &=& 8. \\
\end{array}\right.
\hspace{1cm}
(2)\left\{\begin{array}{rcl}
2x+3y-z &=& 4, \\
x-2y+4z &=& -5, \\
3x+8y-2z &=& 13, \\
4x-y+9z &=& -6. \\
\end{array}\right.
\end{eqnarray*}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\item  
求解线性方程组
\begin{eqnarray*}
\left\{\begin{array}{rcl}
4x_1+x_2&=&1, \\ 
x_1+4x_2+x_3&=&2, \\ 
x_2+4x_3+x_4&=&3, \\ 
\cdots && \cdots \\
x_{998}+4x_{999}+x_{1000}&=&999, \\ 
x_{999}+4x_{1000}&=&1000. \\ 
\end{array}\right.
\end{eqnarray*}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\item  
求下列方程组的符号解和数值解
\begin{eqnarray*}
\left\{\begin{array}{rcl}
x^2-y-x&=&3, \\ 
x+3y&=&2.
\end{array}\right.
\end{eqnarray*}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\item  %10

某容器内侧是由曲线 $x^2+y^2=4y(1\le y\le 3)$ 与 $x^2+y^2=4(y\le 1)$ 绕$y$轴旋转一周而成的曲面。求容器的体积。若将容器内盛满的水从容器顶部全部抽出，至少需要做多少功？

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

解答：先画出容器的图形。这是两个有部分重叠的圆。
先使用极坐标画出两个半径为2的圆周。
然后按题目条件画出一些水平的线段，表示容器内盛水的部分。

\begin{python}
import numpy as np
import matplotlib.pyplot as plt

# 创建一个新的图形
plt.figure()

# 第一个圆 x^2 + (y-2)^2 = 4
theta = np.linspace(0, np.pi*2, 1000)  
x1 = 2 * np.sin(theta)
y1 = 2 + 2 * np.cos(theta)
# 绘制第一个圆
plt.plot(x1, y1)

# 第二个圆 x^2 + y^2 = 4
theta = np.linspace(0, np.pi*2, 1000)
x2 = 2 * np.cos(theta)
y2 = 2 * np.sin(theta)
# 绘制第二个圆
plt.plot(x2, y2)

# 设置坐标轴等比例
plt.axis('equal')
#显示格子
plt.grid(visible=True,axis='both')

#在下面的圆内画30条水平线段
yy1 = np.linspace(-2,1,30)
for y in yy1:
    plt.plot([-np.sqrt(4-y**2),np.sqrt(4-y**2)],[y,y], c='blue')
    
#在上面的圆内画20条水平线段
yy2 = np.linspace(1,3,20)
for y in yy2:
    plt.plot([-np.sqrt(4*y-y**2),np.sqrt(4*y-y**2)],[y,y], c='blue')
\end{python}

\begin{figure}[ht!]
\centering
\includegraphics[height=5cm, width=8cm]{ti-2-10.png}
\caption{第10题的容器储水示意图}
\end{figure}

接下来计算将容器内的所有水提升到顶部，克服重力需要做的功。
注意容器顶部在 $y=3$ 所在的水平线上。
不同水平层位上的水的提升高度是不一样的，
不同水平层位上的水的重量也是不一样的，
因此需要使用积分来计算总的功。
记重力加速度为 $g$. 

正如在画图时所体现的，将纵坐标 $y$ 分成 $[-2,1]$ 与 $[1,3]$ 两个区间。

\begin{enumerate}
\item[(1)]  当 $-2\le y\le 1$ 时，横截面是一个半径为 $ x = \sqrt{4-y^2}$
的圆。其面积为 $$ S(y)=\pi x^2 = \pi(4-y^2). $$
设高度为 $y$ 的一层薄薄的水的厚度为 $dy$. 因此将这部分的水的体积为 
$$
dV = \pi (4-y^2)dy. 
$$

\item[(2)]  当 $1\le y\le 3$ 时，横截面是一个半径为 $ x = \sqrt{4y-y^2}$
的圆。其面积为 $$ S(y)=\pi x^2 = \pi(4y-y^2). $$
高度为 $y$ 的一层薄薄的水的体积为 
$$
dV = \pi (4y-y^2)dy. 
$$

\end{enumerate}

因此这个容器的总的体积为 
$$
V = \int_{-2}^{1} \pi (4-y^2)dy + \int_{1}^{3} \pi (4y-y^2)dy.
$$

将高度为 $y$ 的那层薄薄的水提升到高度为 $3$ 的容器顶部，需要提升的高度为 $3-y$, 
因此所求的总功为 
$$
W = W_1+W_2 = g \int_{-2}^{1} \pi (4-y^2)（3-y)dy + g \int_{1}^{3} \pi (4y-y^2)(3-y)dy.
$$

为计算这个积分，我们使用 Python 的符号计算库 sympy.
其中的 integrate 函数可以计算积分，
Rational(98,10) 是定义了一个符号分式数值 $98/10$. 

\begin{python}
import sympy as sp

#定义符号变量 y, 以及两个符号表达式，
#分别代表高度为 y 的那一层水的面积
sp.var('y')
f1 = sp.pi*(4-y**2)
f2 = sp.pi*(4*y-y**2)

#计算总的体积
V = sp.integrate(f1, (y,-2,1)) + sp.integrate(f2, (y,1,3))

#计算总的重量，其中g=9.8, 每立方米的水设为1000千克
W = 1000*sp.Rational(98,10)\
    *(sp.integrate(f1*(3-y),(y,-2,1))\
    +sp.integrate(f2*(3-y),(y,1,3)))

print('容器的体积为：', V)
print('所做的功为：', W)

#使用Sympy的N函数，计算符号表达式的具体数值
Vnum=sp.N(V)
print(Vnum)

Wnum=sp.N(W)
print(Wnum)
\end{python}

计算结果显示容器的总体积为 
$$ V = \frac{49}{3}\pi. $$
将盛满的水从容器顶部抽出需要做的功为
$$ W =\frac{1075550}{3}\pi. $$

代入 $\pi$ 进行数值计算，可得
总体积为 $V=51.3129$ 立方米，
总功为 $W=1126313.3262$ 焦耳。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\item  
已知 
\begin{eqnarray*}
f(x) &=& (|x+1|-|x-1|)/2 + \sin x, \\
g(x) &=& (|x+3|-|x-3|)/2 + \cos x, 
\end{eqnarray*}
求下列方程组的数值解。
\begin{eqnarray*}
\left\{\begin{array}{rcl}
2x_1 &=& 3f(y_1) +4g(y_2) -1, \\ 
3x_2 &=& 2f(y_1) +6g(y_2) -2, \\ 
y_1 &=& f(x_1) +3g(x_2) -3, \\ 
5y_2 &=& 4f(x_1) +6g(x_2) -1. \\ 
\end{array}\right.
\end{eqnarray*}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\item  
求下列矩阵的特征值和特征向量的数值解和符号解：
\begin{eqnarray*}
\begin{bmatrix}
-1&1&0 \\ 
-4&3&0 \\ 
1&0&2 \\ 
\end{bmatrix}.
\end{eqnarray*}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\item  
已知 
\begin{eqnarray*}
f(x) &=& (|x+1|-|x-1|)/2 + \sin x, \\
g(x) &=& (|x+3|-|x-3|)/2 + \cos x, 
\end{eqnarray*}
求下列超定方程组的最小二乘解。
\begin{eqnarray*}
\left\{\begin{array}{rcl}
2x_1 &=& 3f(y_1) +4g(y_2) -1, \\ 
3x_2 &=& 2f(y_1) +6g(y_2) -2, \\ 
y_1 &=& f(x_1) +3g(x_2) -3, \\ 
5y_2 &=& 4f(x_1) +6g(x_2) -1. \\ 
x_1+y_1 &=& f(y_2) +g(x_2) -2, \\ 
x_2-3y_2 &=& 2f(x_1) -10g(y_1) -5. \\ 
\end{array}\right.
\end{eqnarray*}


\end{enumerate}




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

